package org.jeecg.modules.xj.villager.controller;

import cn.hutool.core.comparator.PinyinComparator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.util.JSONUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.mybatis.MybatisPlusSaasConfig;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.xj.points.entity.XjPoints;
import org.jeecg.modules.xj.points.service.IXjPointsService;
import org.jeecg.modules.xj.qrcode.entity.XjQrCode;
import org.jeecg.modules.xj.qrcode.service.IXjQrCodeService;
import org.jeecg.modules.xj.utils.QRcodeUtil;
import org.jeecg.modules.xj.villager.entity.SysUserVO;
import org.jeecg.modules.xj.villager.service.VillagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* @Description: 积分
* @Author: jeecg-boot
* @Date:   2023-08-11
* @Version: V1.0
*/
@Api(tags="村民信息")
@RestController
@RequestMapping("/villager")
@Slf4j
public class VillagerController extends JeecgController<XjPoints, IXjPointsService> {
   @Autowired
   private IXjPointsService xjPointsService;
   @Autowired
   private VillagerService villagerService;
   @Autowired
   private IXjQrCodeService xjQrCodeService;
   @Autowired
   private ISysUserService sysUserService;
   @Autowired
   private BaseCommonService baseCommonService;

   /**
    * 村民信息查询
    * 汇总所有村民关联的信息
    *
    * @param user
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @RequestMapping(value = "/getUserVillager", method = RequestMethod.GET)
   public Result<IPage<SysUserVO>> getUserVillager(SysUser user, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
      QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());

      return villagerService.getUserVillager(req, queryWrapper, pageSize, pageNo);
   }

   /**
    * 新增村民信息
    * @param jsonObject
    * @param req
    * @return
    */
   @PostMapping(value = "/addVillager")
   public  Result<String> saveUser(@RequestBody JSONObject jsonObject
                         ,HttpServletRequest req) {
      SysUser user = new SysUser();
      Map<String,Object> map = (Map)jsonObject.get("user");
      String selectedRoles = jsonObject.getString("selectedroles");
      String selectedDeparts = jsonObject.getString("selecteddeparts");
      String relTenantIds = jsonObject.getString("relTenantIds");
      try {
         user.setRealname(String.valueOf(map.get("realname")));
         user.setSex(Integer.valueOf(String.valueOf(map.get("sex"))));
         user.setPhone(String.valueOf( map.get("phone")));
         user.setIdCard(String.valueOf( map.get("idCard")));
         user.setHujdz(String.valueOf( map.get("hujdz")));
         user.setUtype(Integer.valueOf(String.valueOf(map.get("utype"))));
         user.setNation(Integer.valueOf(String.valueOf(map.get("nation"))));
         user.setUserIdentity(0);

         sysUserService.saveUser(user,selectedRoles,selectedDeparts,relTenantIds);
         baseCommonService.addLog("添加用户，username： " +user.getUsername() , CommonConstant.LOG_TYPE_2, 2);
      } catch (Exception e){
         return Result.error("添加失败，请联系管理员！");
      }

      return Result.OK("添加成功！");
   }

   /**
    * 生成 二维码
    * @param url
    * @param response
    * @throws Exception
    */
   @GetMapping("code")
   public void qrcode(String url, HttpServletResponse response) throws Exception {
      QRcodeUtil.encode(url, response);
   }


   @GetMapping("qrcodeAll")
   public void qrcodeAll(String checkId, HttpServletResponse response) throws Exception {
      List<XjQrCode> qrCodeList =  xjQrCodeService.list();
      for (XjQrCode xjQrCode:qrCodeList ) {
         villagerService.qrcodeAll(xjQrCode.getId(),response);
      }
   }
}
